<?php

/**
 * 运营平台->删除拍单跟踪日志中的重复数据
 *
 * @author Xmh
 */
date_default_timezone_set("Asia/Shanghai");
set_time_limit(0);
if (isset($_SERVER['REQUEST_URI'])) {
    die('error');
}

if (!defined('API_ROOT_PATH')) {
    define('API_ROOT_PATH', dirname(__FILE__));
}

require_once(API_ROOT_PATH . '/../inc/Config.php');
require_once(API_ROOT_PATH . '/../inc/dblink.php');
require_once(API_ROOT_PATH . '/../inc/Mysql.php');
require_once(API_ROOT_PATH . '/../inc/MysqliDb.php');


//获得数据库操作对象
function get_mysql_handler()
{
    $dbm = new Mysql(MYSQLDB, MYSQLDB_USER, MYSQLDB_PWD, MYSQLDB_DATABASE, 'utf8', false);
    return $dbm;
}


//初始化数据库链接
function init_db()
{
    $db = new MysqliDb([
        'host' => MYSQLDB,
        'username' => MYSQLDB_USER,
        'password' => MYSQLDB_PWD,
        'db' => MYSQLDB_DATABASE,
        'port' => 3306,
        'prefix' => 'au_',
        'charset' => 'utf8'
    ]);
    return $db;
}

function getNextSleepSecond()
{
    $date1 = time();
    $date2 = date('Y-m-d H:i:s', strtotime('+1 hour', $date1));

    $date2 = strtotime($date2);
    $s = $date2 - $date1;

    $date1 = date('Y-m-d H:i:s', $date1);
    $date2 = date('Y-m-d H:i:s', $date2);

    logUtil("current_time:$date1  next_time:$date2  timediff:$s", 'INFO');

    return $s;
}


//初始化redis
function init_redis()
{
    $redis = new Redis();
    $redis->connect(REDIS_HOST, REDIS_PORT);
    return $redis;
}

$db = init_db();
$dbh = get_mysql_handler();
$redis = init_redis();

//判断数据库链接状态
if (!$db->ping()) {
    $db = init_db();
}

$counter = 0;
$order_trace_log_list = $db->where('order_id', 34756, '<=')->orderBy('id', 'ASC')->groupBy('order_id')
    ->get('order_trace_log_list', null, 'order_id,car_id');
$count_list = count($order_trace_log_list);
logUtil("共" . $count_list . "条记录", 'INFO');
if (!empty($order_trace_log_list)) {
//    $db->startTransaction();
    foreach ($order_trace_log_list as $k => $v) {
        $sql = "SELECT id FROM (SELECT MAX(id) AS id, COUNT(createtime) AS ucount FROM au_order_trace_log_list WHERE order_id = {$v['order_id']} AND car_id = {$v['car_id']} GROUP BY createtime HAVING ucount > 1) AS tab";
        $result = $db->query($sql);
        if (!empty($result)) {
            foreach ($result as $rk => $rv) {
                $affect_rows = $db->where('id', $rv['id'])->delete('order_trace_log_list');
                if ($affect_rows) {
//                    $db->commit();
                    logUtil("order_id: " . $v['order_id'] . "car_id：" . $v['car_id'] . "删除ID：" . $rv['id'] . ' 处理成功', 'INFO');
                    $counter++;
                } else {
//                    $db->rollback();
                    logUtil("order_id: " . $v['order_id'] . "car_id：" . $v['car_id'] . "删除ID：" . $rv['id'] . ' 处理失败', 'INFO');
                }
            }
        } else {
            logUtil("order_id: " . $v['order_id'] . "car_id：" . $v['car_id'] . " 无重复数据", 'INFO');
        }
    }
}
logUtil($counter . " 条数据处理完成", "INFO");

/**
 * 日志记录方法
 *
 * @param        $msg
 * @param string $level DEBUG,INFO,WARN,ERR
 *
 * @return bool
 */
function logUtil($msg, $level = "INFO")
{

    $cost_mem = memory_usage();
    $log = "[{$level}][" . date('Y-m-d H:i:s') . "][{$cost_mem}]\t{$msg}\n";
    echo $log;
    unset($log);
}

function memory_usage()
{
    $memory = (!function_exists('memory_get_usage')) ? '0' : round(memory_get_usage() / 1024 / 1024, 2) . 'MB';
    return $memory;
}
